package io.mycat.eye.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.mycat.eye.web.bean.MycatServer;
import io.mycat.eye.web.dto.RestResponse;
import io.mycat.eye.web.service.MycatServerService;

/**
 * Created by xqy on 18/5/1.
 */
@RestController
@RequestMapping("/mycat/server")
public class MycatServerController {
    Logger logger = LoggerFactory.getLogger(MycatServerController.class);

    @Autowired
    MycatServerService mycatServerService;


    @GetMapping(value = "list")
    public ResponseEntity<RestResponse<Object>> getServerList(){
        logger.debug("list");
        RestResponse<Object> response = RestResponse.buildExceptionResponse(200,"successful");
        try{
            response.setData(mycatServerService.getMycatServer());
            return new ResponseEntity<>(response,HttpStatus.OK);
        }catch (Exception e){
            response.setCode(700);
            response.setMessage(e.getMessage());
            return new ResponseEntity<>(response,HttpStatus.OK);
        }
    }

    // 编辑节点
    @GetMapping(value = "detail/{id}")
    public ResponseEntity<RestResponse<Object>> getServer(@PathVariable Integer id){
        RestResponse<Object> response = RestResponse.buildExceptionResponse(200, "ok");
        try{
            response.setData(mycatServerService.getServer(id));
            return new ResponseEntity<>(response,HttpStatus.OK);
        }catch (Exception e){
            response.setCode(700);
            response.setMessage(e.getMessage());
            return new ResponseEntity<>(response,HttpStatus.OK);
        }
    }

    // 新增节点
    @PostMapping(value = "detail", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<RestResponse<Object>> updateServer(@RequestBody MycatServer server){
        RestResponse<Object> response = RestResponse.buildExceptionResponse(200, "ok");
        try{
            long curTime = System.currentTimeMillis();
            server.setCreateTime(curTime);
            server.setLastUpdateTime(curTime);
            mycatServerService.addServer(server);
            return new ResponseEntity<>(response,HttpStatus.OK);
        }catch (Exception e){
            response.setCode(700);
            response.setMessage(e.getMessage());
            return new ResponseEntity<>(response,HttpStatus.OK);
        }
    }

    // 编辑节点
    @PutMapping(value = "detail/{id}", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<RestResponse<Object>> getServer(@PathVariable Integer id, @RequestBody MycatServer server){
        RestResponse<Object> response = RestResponse.buildExceptionResponse(200, "ok");
        try{
            if(server.getId() == null){
                server.setId(id);
            }
            mycatServerService.updateServer(server);
            return new ResponseEntity<>(response,HttpStatus.OK);
        }catch (Exception e){
            response.setCode(700);
            response.setMessage(e.getMessage());
            return new ResponseEntity<>(response,HttpStatus.OK);
        }
    }

    // 删除节点
    @DeleteMapping(value = "detail/{id}")
    public ResponseEntity<RestResponse<Object>> deleteServer(@PathVariable Integer id){
        RestResponse<Object> response = RestResponse.buildExceptionResponse(200, "ok");
        try{
            mycatServerService.delete(id);
            return new ResponseEntity<>(response,HttpStatus.OK);
        }catch (Exception e){
            response.setCode(700);
            response.setMessage(e.getMessage());
            return new ResponseEntity<>(response,HttpStatus.OK);
        }
    }



}
